package com.guodu.service.impl;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.guodu.dao.MaterialRukuDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.MaterialRuku;
import com.guodu.entity.MaterialrukuStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.MaterialRukuService;
import com.guodu.vo.MaterialRukuQueryVo;
import com.guodu.vo.MaterialRukuVo;

@Service("materialRukuService")
public class MaterialRukuServiceImpl implements MaterialRukuService {

	@Resource
	private MaterialRukuDao materialRukuDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public List<MaterialRukuVo> findByVo(MaterialRukuQueryVo materialRukuQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<MaterialRuku> list = materialRukuDao.findByVo(materialRukuQueryVo,units, curPage, pageSize);
		List<MaterialRukuVo> materialRukuVos = new ArrayList<MaterialRukuVo>();
		for(MaterialRuku materialRuku : list ){
			MaterialRukuVo materialRukuVo = new MaterialRukuVo(materialRuku);
			UserInfo userInfo = userInfoDao.findById(materialRuku.getCreatePeople());
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			materialRukuVo.setUnitsName(unitsInfo.getUnitsName());
			materialRukuVo.setUnitsId(unitsInfo.getUnitsId());
			materialRukuVo.setIndustry(unitsInfo.getIndustry());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				materialRukuVo.setFunitsName(funitsInfo.getUnitsName());
				materialRukuVo.setFunitsId(funitsInfo.getUnitsId());
			}
			materialRukuVo.setUserName(userInfo.getUserName());
			materialRukuVos.add(materialRukuVo);
		}
		return materialRukuVos;
	}

	@Override
	public int findSizeByVo(MaterialRukuQueryVo materialRukuQueryVo,Units units) {
		// TODO Auto-generated method stub
		return materialRukuDao.findSizeByVo(materialRukuQueryVo,units);
	}

	@Override
	public MaterialRuku findById(String id) {
		// TODO Auto-generated method stub
		return materialRukuDao.findById(id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		materialRukuDao.del(id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		materialRukuDao.delByIds(ids);
	}

	@Override
	public void save(MaterialRuku entity) {
		// TODO Auto-generated method stub
		materialRukuDao.save(entity);
	}

	@Override
	public void examine(String ids,String reason,String beforeStatus,Long afterStatus,UserInfo userInfo) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<MaterialrukuStatus> entities = new ArrayList<MaterialrukuStatus>();
		for(int i=0;i<idses.length;i++){
			MaterialrukuStatus entity = new MaterialrukuStatus();
			entity.setAfterStatus(afterStatus);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(userInfo.getUserId());
			entity.setMaterialId(idses[i]);
			entity.setReason(reason);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		materialRukuDao.changeStatus(entities);
		materialRukuDao.examine(ids,afterStatus);
	}

	@Override
	public void update(MaterialRuku entity) {
		// TODO Auto-generated method stub
		materialRukuDao.update(entity);
	}

	@Override
	public List<MaterialrukuStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return materialRukuDao.findStatus(id);
	}

	@Override
	public List<MaterialRuku> findAllByVo(
			MaterialRukuQueryVo materialRukuQueryVo, Units units) {
		// TODO Auto-generated method stub
		return materialRukuDao.findAllByVo(materialRukuQueryVo, units);
	}

	@Override
	public BigDecimal findAllMoney(MaterialRukuQueryVo materialRukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialRukuDao.findAllMoney(materialRukuQueryVo,
			 units);
	}

	@Override
	public boolean examinAll(MaterialRukuQueryVo materialRukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialRukuDao.examinAll(materialRukuQueryVo,units);
	}

	@Override
	public boolean allBack(MaterialRukuQueryVo materialRukuQueryVo, Units units) {
		// TODO Auto-generated method stub
		return materialRukuDao.allBack(materialRukuQueryVo,units);
	}

	@Override
	public boolean huituiAll(MaterialRukuQueryVo materialRukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialRukuDao.huituiAll(materialRukuQueryVo,units);
	}

	@Override
	public boolean shanchuAll(MaterialRukuQueryVo materialRukuQueryVo,
			Units units) {
		
		return materialRukuDao.shanchuAll(materialRukuQueryVo,units);
	}



}
